package com.rocching.easychat.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rocching.easychat.R;
import com.rocching.easychat.core.ViewClickListener;
import com.rocching.easychat.model.AgreeUserListModel;
import com.rocching.easychat.model.AgreeUserModel;
import com.rocching.easychat.util.AppUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class NewFriendListAdapter extends BaseAdapter {
    private List<AgreeUserModel> list;
    private final Context context;
    private final BtnClickListener clickListener;

    public NewFriendListAdapter(Context context, BtnClickListener btnClickListener) {
        this.context = context;
        this.clickListener = btnClickListener;
        list = new ArrayList<>();
    }

    public void update(List<AgreeUserModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        AgreeUserModel model = list.get(position);
        if (Objects.isNull(model)) {
            return 0;
        }
        return model.getId();
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        NewFriendListHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_new_friend_list_item, null);
            holder = new NewFriendListHolder(view, clickListener);
            view.setTag(holder);
        } else {
            holder = (NewFriendListHolder) view.getTag();
        }
        AgreeUserModel model = list.get(position);
        if (!Objects.isNull(model)) {
            holder.update(model);
        }
        return view;
    }

    public static class NewFriendListHolder {
        private final View view;
        private final ImageView friendPhoto;
        private final TextView friendName;
        private final TextView statusName;
        private final TextView greetMsg;
        private final LinearLayout btnLayout;
        private AgreeUserModel model;

        public NewFriendListHolder(View view, BtnClickListener clickListener) {
            this.view = view;

            friendPhoto = view.findViewById(R.id.friend_photo);
            friendName = view.findViewById(R.id.friend_name);
            statusName = view.findViewById(R.id.status_name);
            greetMsg = view.findViewById(R.id.greet_msg);
            btnLayout = view.findViewById(R.id.btn_layout);

            Button btnAgree = view.findViewById(R.id.btn_agree);
            Button btnReject = view.findViewById(R.id.btn_reject);

            btnReject.setOnClickListener(new ViewClickListener() {
                @Override
                public void onDClick(View v) {
                    if (clickListener != null) {
                        clickListener.onClick(0, model);
                    }
                }
            });

            btnAgree.setOnClickListener(new ViewClickListener() {
                @Override
                public void onDClick(View v) {
                    if (clickListener != null) {
                        clickListener.onClick(1, model);
                    }
                }
            });
        }

        public void update(AgreeUserModel model) {
            this.model = model;
            model.setStatusName(AppUtil.getStatusName(view.getContext(), model.getStatus(), model.getId()));
            friendName.setText(model.getName());
            statusName.setText(model.getStatusName());
            greetMsg.setText(model.getGreetMsg());

            Uri uri = AppUtil.getUserPhotoUri(model.getPhoto());
            if (uri != null) {
                Glide.with(view).load(uri).placeholder(R.drawable.loading).into(friendPhoto);
            }

            Integer status = model.getStatus();
            if (Objects.isNull(status)) {
                status = -1;
            }
            if (status == 0) {
                btnLayout.setVisibility(View.VISIBLE);
                statusName.setVisibility(View.GONE);
            } else {
                btnLayout.setVisibility(View.GONE);
                statusName.setVisibility(View.VISIBLE);
            }
        }
    }

    public interface BtnClickListener {
        /**
         * 点击
         *
         * @param action 1=同意 0=拒绝
         */
        void onClick(int action, AgreeUserModel model);
    }
}
